package fileoperation

import java.io.Closeable
import java.io.File
import java.io.RandomAccessFile

object FileOperation {

    fun readFile(file: File): ByteArray? {
        var byteArray: ByteArray? = null
        var randomAccessFile: RandomAccessFile? = null
        try {
            randomAccessFile = RandomAccessFile(file, "r")
            byteArray = ByteArray(randomAccessFile.length().toInt())
            randomAccessFile.readFully(byteArray)
        } catch (ex: Exception) {
            ex.printStackTrace()
        } finally {
            closeFile(randomAccessFile)
        }
        return byteArray
    }

    fun closeFile(closeable: Closeable?) {
        try {
            closeable?.close()
        } catch (ex: Exception) {
            ex.printStackTrace()
        }
    }
}